package com.sky.controller.admin;

import com.sky.annotations.AutoFill;
import com.sky.constant.JwtClaimConstant;
import com.sky.controller.BaseController;
import com.sky.dto.EmployDTO;
import com.sky.dto.EmployLoginDTO;
import com.sky.entity.EmployeeEntity;
import com.sky.enumeration.EmployeeStatusEnum;
import com.sky.enumeration.OperationType;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.result.TableDataInfo;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtils;
import com.sky.utils.TransformUtils;
import com.sky.vo.EmployeeVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
@Api(tags = "员工模块")
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    @ApiOperation(value = "员工登录", notes = "员工登录notes")
    public Result login(@RequestBody EmployLoginDTO dto) {
        log.info("员工登录信息: {}", dto);
        EmployeeEntity employee = employeeService.login(dto);

        HashMap<String, Object> claim = new HashMap<>();
        claim.put(JwtClaimConstant.EMP_ID, employee.getId());
        String token = JwtUtils.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claim);

        EmployeeVO employeeVO = EmployeeVO.builder().id(employee.getId()).username(employee.getUsername()).name(employee.getName()).token(token).build();
        return Result.success("登录成功", employeeVO);
    }

    @PostMapping
    @ApiOperation(value = "新增员工")
    public Result save(@RequestBody EmployDTO dto) {
        log.info("新增员工信息：{}", dto);
        employeeService.save(dto);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "name", value = "员工姓名", required = false),
                    @ApiImplicitParam(name = "page", value = "分页", required = false),
                    @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false)}
    )
    public TableDataInfo page(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "page",required = false) Integer page, @RequestParam(value = "pageSize",required = false) Integer size) {
        List<EmployeeEntity> list = employeeService.page(name, page, size);
        return getTableInfo(list);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用，禁用员工")
    public Result enableOrDisable(@PathVariable("status") String status,Long id){
        EmployeeStatusEnum statusEnum = TransformUtils.transformEnum(status, EmployeeStatusEnum.class);
        employeeService.enableOrDisable(statusEnum,id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result getById(@PathVariable Long id){
       EmployeeEntity entity =  employeeService.getById(id);
        return Result.success(entity);
    }

    @PutMapping
    @ApiOperation("编辑用户信息")
    public Result update(@RequestBody EmployDTO employDTO){
        log.info("编辑用户信息,{}",employDTO);
        employeeService.update(employDTO);
        return Result.success();
    }

}
